/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package my.cbr.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import my.cbr.database.model.FishDeath;
import my.cbr.database.model.HatcheryCompanyID;
import my.cbr.database.model.SpeciesOrigin;
import my.cbr.database.model.Vaccine;
import my.cbr.database.model.measurement.MeasurementInstance;

/**
 *
 * @author Geir Ole
 */
public class Utility {

    /**
     *
     * @param <T>
     * @param enumValues
     * @return 
     */
    public static <T extends Enum<T>> String[] enumToStringArray(T[] enumValues)
    {
        int i = 0;
        String[] result = new String[enumValues.length];
        for (T value: enumValues) {
            result[i++] = value.name();
        }
        return result;
    }

    public static <E extends MeasurementInstance> Double[] listOfMeasurementInstanceToDoubleArray(List<E> list)
    {
        Double[] result = new Double[list.size()];
        int i = 0;
        for(E mi : list)
            result[i++] = Double.parseDouble(mi.getValue()+"");
        return result;
    }
    
    public static Map<String,Integer> getAggregatedCount(List<FishDeath> fishdeath) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        for(FishDeath fd : fishdeath) {
            String cause = fd.getCause();
            Integer count = fd.getCount();
            Integer previous = map.get(cause);
            if(previous!=null)
                count += previous;
            map.put(cause, count);
        }
        return map;
    }

        public static String getString(Object[] list)
    {
        String result = "";
        int length = list.length;
        if(length==0)
            result = "Nothing registered";

        for (int i = 0; i < length; i++) {
            if(i > 0)
               result +=", ";

            if(list[i] instanceof SpeciesOrigin)
            {
                result += ((SpeciesOrigin)list[i]).getSpeciesName();
            }
            else if(list[i] instanceof Vaccine)
            {
                result += ((Vaccine)list[i]).getName();
            }
            else if(list[i] instanceof HatcheryCompanyID)
            {
                result += ((HatcheryCompanyID)list[i]).getHatcheryID().toString();
            }

        }

      return result;
    }


}
